package com.xintong.visualinspection.service.impl;

import com.xintong.visualinspection.bean.Permission;
import com.xintong.visualinspection.bean.User;
import com.xintong.visualinspection.dao.cluster.UserInfoDao;
import com.xintong.visualinspection.dao.master.PermissionDao;
import com.xintong.visualinspection.dao.master.UserDao;
import com.xintong.visualinspection.pojo.SimpleGrantedAuthority;
import com.xintong.visualinspection.service.BaseService;
import com.xintong.visualinspection.service.UserService;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.xintong.visualinspection.bean.Permission;
import com.xintong.visualinspection.bean.User;
import com.xintong.visualinspection.dao.cluster.UserInfoDao;
import com.xintong.visualinspection.dao.master.PermissionDao;
import com.xintong.visualinspection.dao.master.UserDao;
import com.xintong.visualinspection.service.BaseService;
import com.xintong.visualinspection.service.UserService;

/**
 * 文件名：UserServiceImpl
 * 版本信息：日期：2017/3/30 Copyright 江苏省交通规划设计院 Corporation 2017 版权所有.
 */
@Service
@Data
public class UserServiceImpl extends BaseService implements UserService {

    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(UserService.class);


    @Autowired
    private UserDao userDao;

    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private PermissionDao permissionDao;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<User> getAll() {
        return userInfoDao.getAll();
    }
    
	@Override
	public List<User> getUsers(User user) {
		return userInfoDao.getUsers(user);
	}


    @Override
//    @Cacheable(value = "usercache",keyGenerator = "keyGenerator")
    public User getOne(Integer id) {
    	return userInfoDao.getOne(id);
    }

    @Override
    public void insert(User user) {
    	userInfoDao.insert(user);
    }

    @Override
    public void update(User user) {
    	userInfoDao.update(user);
    }

    @Override
    public void delete(Integer id) {
    	userInfoDao.delete(id);
    }


    @Override
    public UserDetails loadUserByUsername(String username) {
        User user = userInfoDao.findByUserName(username);
        if (user != null) {
            List<Permission> permissions = permissionDao.findByAdminUserId(user.getId());
            List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
            for (Permission permission : permissions) {
                if (permission != null && permission.getName() != null) {

                    GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(permission.getName());
                    grantedAuthorities.add(grantedAuthority);
                }
            }
            user.setGrantedAuthorities(grantedAuthorities);
            return user;
        } else {
            throw new UsernameNotFoundException("admin: " + username + " do not exist!");
        }

    }


}
